package repository

import (
	"yak.admin/src/entity"
	"yak.admin/src/service"
	"yak.admin/src/util"
)

type ReportDiscussRepository struct {
}

func (repos *ReportDiscussRepository) FindAllByPid(userId uint, pid uint, page int, pagesize int) []map[string]interface{} {
	reader := new(service.ReaderQuery).CreateModelQuery(&entity.ReportDiscuss{}).
		WithEQ("user_id", userId).
		WithEQ("pid", pid).
		WithPage(page).
		WithPageSize(pagesize).
		WithPreloads([]string{"User"})
	var discusses []*entity.ReportDiscuss

	db := util.GetMysqlPool().Handler()
	reader.Reader(&discusses)
	return reader.Datalist(discusses, len(discusses), func(index int, item interface{}) map[string]interface{} {
		m := item.(*entity.ReportDiscuss)
		var annexes []entity.DeportDiscussAnnex
		db.Where("pid = ?", m.Id).Find(&annexes)
		return map[string]interface{}{
			"avatar":     util.GetImage(m.User.Avatar),
			"message":    m.Message,
			"imgs":       reader.AsListImg(annexes, len(annexes), "Src"),
			"created_at": m.CreatedAt,
		}
	})
}
